package com.yanqu.road.server.cmd.activity.peakseacraft;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakseacraft.config.PeakSeacraftActivityConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PeakSeacraftProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.peakseacraft.PeakSeacraftModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.protocol.*;

@Cmd(code = GameProtocol.S_PEAK_SEACRAFT_GET_GROUP_UNION_LINST, desc = "获取分组内船只列表")
public class PeakSeacraftGetGroupUnionListCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PeakSeacraftProto.PeakSeacraftGetGroupDetailReqMsg reqMsg = PeakSeacraftProto.PeakSeacraftGetGroupDetailReqMsg.parseFrom(packet.getBytes());
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (null == activityInfo){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        PeakSeacraftProto.PeakSeacraftGetGroupUnionListGameToCrossMsg.Builder crossMsg = PeakSeacraftProto.PeakSeacraftGetGroupUnionListGameToCrossMsg.newBuilder();
        crossMsg.setActivityId(reqMsg.getActivityId());
        crossMsg.setUnionId(player.getUserInfo().getUnionUid());
        crossMsg.setCtsMsg(reqMsg);

        if (activityInfo.getType() == eActivityType.PeakSeacraftActivity.getValue()) {
            ret = PeakSeacraftMgr.canEntryActivity(player, reqMsg.getActivityId());
            if (ret != 0) {
                return;
            }
            PeakSeacraftActivityConfig config = PeakSeacraftMgr.getConfig();
            if (null == config) {
                ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_OPEN;
                return;
            }
            // 判断是否在活动期
            if (!config.isOpenTime()) {
                ret = GameErrorCode.E_PEAK_SEACRAFT_ACTIVITY_NO_FIGHT_TIME;
                return;
            }
            PeakSeacraftMgr.enter(player.getUserId(), reqMsg.getArea(), reqMsg.getGroup());

            player.sendUnionActivityGroupPacket(CrossProtocol.C_PEAK_SEACRAFT_GET_GROUP_UNION_LINST, crossMsg, crossMsg.getActivityId());
        } else {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            PeakSeacraftProto.PeakSeacraftGetGroupDetailRespMsg.Builder resp = PeakSeacraftProto.PeakSeacraftGetGroupDetailRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_PEAK_SEACRAFT_GET_GROUP_UNION_LINST, resp);
        }
    }
}
